module key_board (
	input sys_clk, 
	input sys_rst,
	input [3:0] key,
	output reg [3:0] output_key_id);
	
reg [31:0] key_debonse_timer;

parameter [3:0]  KEY_ID_IDLE = 0;
parameter [3:0] KEY_ID_KEY1 = 1;
parameter [3:0] KEY_ID_KEY2 = 2;
parameter [3:0] KEY_ID_KEY3 = 3;
parameter [3:0] KEY_ID_KEY4 = 4;

always @(posedge sys_clk or negedge sys_rst) begin 
	if (!sys_rst) begin 			//复位
		key_debonse_timer <= 0;
		output_key_id <= KEY_ID_IDLE;
	end
	else 	begin
		if (key != 4'b1111)					begin 				//有按键按下
			key_debonse_timer <= key_debonse_timer + 1;
			if (key_debonse_timer >= 32'd10000000)  begin	//10m/50m = 0.2ms延时去抖
				case (key)
					4'b1110 : output_key_id = KEY_ID_KEY1;
					4'b1101 : output_key_id = KEY_ID_KEY2;
					4'b1011 : output_key_id = KEY_ID_KEY3;
					4'b0111 : output_key_id = KEY_ID_KEY4;
					default : output_key_id = KEY_ID_IDLE;
					
			endcase	
			end
		end
		else	begin 													//无按键按下
			key_debonse_timer <= 0;
			output_key_id <= KEY_ID_IDLE;
		end
	end
 
end

endmodule